package com.zeng.sys.filemd.servicemodule.service.serviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeng.sys.filemd.servicemodule.entity.FileServer;
import com.zeng.sys.filemd.servicemodule.mapper.FileServerMapper;
import com.zeng.sys.filemd.servicemodule.service.FileServerService;
import com.zeng.sys.filemd.servicemodule.vo.FileServerVo;
import com.zeng.sys.filemd.servicemodule.wrapperImpl.FileServerWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FileServerServiceImpl extends ServiceImpl<FileServerMapper, FileServer> implements FileServerService {

    @Autowired
    FileServerMapper fileServerMapper;

    private FileServerWrapper fileServerWrapper = new FileServerWrapper();

    @Override
    public FileServerVo addOne(FileServerVo fileServerVo) {
        FileServer entity = fileServerVo.convertToFileServer();
        save(entity);
        return FileServerVo.convertToFileServerVo(entity);
    }

    @Override
    public FileServerVo getOneByCode(String code) {
        return FileServerVo.convertToFileServerVo(getById(code));
    }

    @Override
    public Integer update(FileServerVo fileServerVo) {
        return this.fileServerMapper.updateById(fileServerVo.convertToFileServer());
    }

    @Override
    public Integer delete(String code) {
        return this.fileServerMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {
        return this.fileServerMapper.deleteBatchIds(codes);
    }

    @Override
    public List<FileServerVo> findPage(Page page, FileServerVo fileServerVo) {
        this.fileServerMapper.selectPage(page, fileServerWrapper.getWrapper(fileServerVo));
        List<FileServerVo> list = (List<FileServerVo>)FileServerVo.listFileServerVo(page.getRecords());
        page.setRecords(null);
        return list;
    }

    @Override
    public Integer count(FileServerVo fileServerVo) {
        return this.fileServerMapper.selectCount(fileServerWrapper.getWrapper(fileServerVo));
    }

    @Override
    public List<FileServerVo> findAll(FileServerVo fileServerVo) {
        return FileServerVo.listFileServerVo(list(fileServerWrapper.getWrapper(fileServerVo)));
    }

    @Override
    public Integer saveBatch(List<FileServerVo> fileServerVos) {
        saveBatch(FileServerVo.listFileServer(fileServerVos));
        return fileServerVos.size();
    }

    @Override
    public boolean updateOrSaveBatch(List<FileServerVo> fileServerVos) {
        return saveOrUpdateBatch(FileServerVo.listFileServer(fileServerVos));
    }

    @Override
    public boolean updateBatch(List<FileServerVo> fileServerVos) {
        return updateBatchById(FileServerVo.listFileServer(fileServerVos));
    }


}
